<template>
  <div class="login-container">
    <div class="login-content">
      <div class="login-left">
        <div class="welcome-text">
          <h1>欢迎使用</h1>
          <h2>儿童英语学习网站</h2>
          <p>让每个乡村孩子都能享受优质的英语教育</p>
        </div>
        <div class="feature-list">
          <div class="feature-item">
            <i class="icon">📚</i>
            <span>个性化学习体验</span>
          </div>
          <div class="feature-item">
            <i class="icon">👨‍👩‍👧‍👦</i>
            <span>家校互动平台</span>
          </div>
          <div class="feature-item">
            <i class="icon">🎯</i>
            <span>智能学习跟踪</span>
          </div>
        </div>
      </div>

      <div class="login-box">
        <div class="login-header">
          <h3>账号登录</h3>
          <p class="sub-title">请选择您的身份登录系统</p>
        </div>

        <form class="login-form" @submit.prevent="handleLogin">
          <div class="form-item">
            <div class="input-wrapper">
              <i class="icon">👤</i>
              <input type="text" v-model="loginForm.username" placeholder="请输入用户名" required />
            </div>
            <span class="error-message" v-if="errors.username">{{ errors.username }}</span>
          </div>

          <div class="form-item">
            <div class="input-wrapper">
              <i class="icon">🔒</i>
              <input type="password" v-model="loginForm.password" placeholder="请输入密码" required />
            </div>
            <span class="error-message" v-if="errors.password">{{ errors.password }}</span>
          </div>

          <button type="submit" class="login-btn">
            登录
            <i class="icon">→</i>
          </button>
        </form>

        <div class="demo-accounts">
          <h3>快速体验账号</h3>
          <div class="account-list">
            <div class="account-item" @click="fillDemoAccount('student')">
              <div class="account-icon">👨‍🎓</div>
              <div class="account-info">
                <h4>学生账号</h4>
                <p>student001 / 123456</p>
              </div>
            </div>
            <div class="account-item" @click="fillDemoAccount('parent')">
              <div class="account-icon">👨‍👩‍👧‍👦</div>
              <div class="account-info">
                <h4>家长账号</h4>
                <p>parent001 / 123456</p>
              </div>
            </div>
            <div class="account-item" @click="fillDemoAccount('teacher')">
              <div class="account-icon">👩‍🏫</div>
              <div class="account-info">
                <h4>教师账号</h4>
                <p>teacher001 / 123456</p>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 消息提示 -->
    <div class="message" v-if="message.show" :class="message.type">
      {{ message.content }}
    </div>
  </div>
</template>

<script>
import { login } from '@/api'
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      errors: {
        username: '',
        password: ''
      },
      message: {
        show: false,
        content: '',
        type: ''
      },
      demoAccounts: {
        student: { username: 'student001', password: '123456' },
        parent: { username: 'parent001', password: '123456' },
        teacher: { username: 'teacher001', password: '123456' }
      }
    }
  },
  methods: {
    fillDemoAccount(type) {
      const account = this.demoAccounts[type]
      this.loginForm.username = account.username
      this.loginForm.password = account.password
      // 添加高亮动画效果
      const inputs = document.querySelectorAll('.input-wrapper')
      inputs.forEach((input) => {
        input.classList.add('highlight')
        setTimeout(() => input.classList.remove('highlight'), 1000)
      })
    },
    showMessage(content, type = 'success') {
      this.message = {
        show: true,
        content,
        type
      }
      setTimeout(() => {
        this.message.show = false
      }, 3000)
    },
    validateForm() {
      let isValid = true
      this.errors = {
        username: '',
        password: ''
      }

      if (!this.loginForm.username.trim()) {
        this.errors.username = '请输入用户名'
        isValid = false
      }
      if (!this.loginForm.password.trim()) {
        this.errors.password = '请输入密码'
        isValid = false
      }

      return isValid
    },
    async handleLogin() {
      if (!this.validateForm()) return

      try {
        const { data } = await login(this.loginForm)

        if (data.length > 0) {
          const user = data[0]
          localStorage.setItem('user', JSON.stringify(user))

          switch (user.role) {
            case 'student':
              this.$router.push('/student')
              break
            case 'parent':
              this.$router.push('/parent')
              break
            case 'teacher':
              this.$router.push('/teacher')
              break
          }

          this.showMessage('登录成功')
        } else {
          this.showMessage('用户名或密码错误', 'error')
        }
      } catch (error) {
        this.showMessage('登录失败，请稍后重试', 'error')
      }
    }
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #1e9fff 0%, #4158d0 100%);
  padding: 20px;
}

.login-content {
  display: flex;
  width: 1000px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 20px;
  overflow: hidden;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.login-left {
  flex: 1;
  padding: 60px 40px;
  background: linear-gradient(45deg, #2196f3, #4caf50);
  color: white;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.welcome-text h1 {
  font-size: 36px;
  margin-bottom: 20px;
}

.welcome-text h2 {
  font-size: 24px;
  margin-bottom: 15px;
}

.welcome-text p {
  font-size: 16px;
  opacity: 0.9;
}

.feature-list {
  margin-top: 60px;
}

.feature-item {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.feature-item .icon {
  font-size: 24px;
  margin-right: 15px;
}

.login-box {
  flex: 1;
  padding: 40px;
  background: white;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h3 {
  font-size: 24px;
  color: #333;
  margin-bottom: 10px;
}

.sub-title {
  color: #666;
  font-size: 14px;
}

.input-wrapper {
  display: flex;
  align-items: center;
  border: 1px solid #dcdfe6;
  border-radius: 8px;
  padding: 0 15px;
  transition: all 0.3s;
}

.input-wrapper:focus-within {
  border-color: #409eff;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.1);
}

.input-wrapper.highlight {
  animation: highlight 1s ease;
}

@keyframes highlight {
  0%,
  100% {
    border-color: #dcdfe6;
  }
  50% {
    border-color: #409eff;
  }
}

.input-wrapper .icon {
  font-size: 20px;
  color: #909399;
  margin-right: 10px;
}

.form-item {
  margin-bottom: 20px;
}

.form-item input {
  flex: 1;
  padding: 12px 0;
  border: none;
  outline: none;
  font-size: 14px;
}

.login-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  padding: 12px 20px;
  background: #409eff;
  border: none;
  border-radius: 8px;
  color: white;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s;
  margin-top: 30px;
}

.login-btn:hover {
  background: #66b1ff;
  transform: translateY(-1px);
}

.login-btn .icon {
  margin-left: 10px;
  transition: transform 0.3s;
}

.login-btn:hover .icon {
  transform: translateX(5px);
}

.demo-accounts {
  margin-top: 40px;
}

.demo-accounts h3 {
  text-align: center;
  color: #606266;
  margin-bottom: 20px;
}

.account-list {
  display: grid;
  gap: 15px;
}

.account-item {
  display: flex;
  align-items: center;
  padding: 15px;
  background: #f8f9fa;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s;
}

.account-item:hover {
  background: #ecf5ff;
  transform: translateY(-2px);
}

.account-icon {
  font-size: 24px;
  margin-right: 15px;
}

.account-info h4 {
  font-size: 14px;
  color: #303133;
  margin-bottom: 5px;
}

.account-info p {
  font-size: 12px;
  color: #909399;
}

.message {
  position: fixed;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  padding: 12px 24px;
  border-radius: 8px;
  color: white;
  font-size: 14px;
  z-index: 1000;
  animation: slideDown 0.3s ease-in-out;
  background: orange;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

/* 响应式适配 */
@media (max-width: 1024px) {
  .login-content {
    width: 90%;
    flex-direction: column;
  }

  .login-left {
    padding: 30px;
  }

  .feature-list {
    margin-top: 30px;
  }
}

@media (max-width: 480px) {
  .login-content {
    width: 100%;
    border-radius: 0;
  }

  .login-left {
    display: none;
  }
}
</style>
